package cn.java79.quiz.service;

import cn.java79.quiz.dao.QuestionsMapper;
import cn.java79.quiz.dao.UserMapper;
import cn.java79.quiz.entity.Questions;
import cn.java79.quiz.entity.User;
import cn.java79.quiz.pojo.AjaxPage;
import cn.java79.quiz.util.LogUtil;
import cn.java79.quiz.util.MybatisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class UserService {
    public static final int PAGESIZE = 10;
    public static UserService ME = new UserService();

    public static void main(String[] args) {
        System.out.println(ME.search("a", null, null, 1, 10));
    }
//    @Deprecated//不建议使用
//    public List<User> search() {
//        SqlSession s = MybatisUtil.get();
//        try {
//            UserMapper mapper = s.getMapper(UserMapper.class);
//            List<User> list = mapper.search();
//            return list;
//        } finally {
//            MybatisUtil.close(s);
//        }
//    }

    /**
     * @return
     */
    public AjaxPage<User> search(String loginname, String realname, String phone, int pageNo, int pageSize) {
        SqlSession s = MybatisUtil.get();
        Page<User> page = PageHelper.startPage(pageNo, pageSize);
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            mapper.search(loginname, realname, phone);

            return new AjaxPage<User>(page.subList(0, page.size()), page.getTotal(), page.getPageNum(), page.getPageSize(), page.getPages());
        } finally {
            MybatisUtil.close(s);
        }
    }

    public AjaxPage<User> search(String loginname, String realname, String phone, int pageNo) {
        return this.search(loginname, realname, phone, pageNo, PAGESIZE);
    }

    public User get(Long id) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            return mapper.get(id);
        } finally {
            MybatisUtil.close(s);
        }
    }

    public int save(User user) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            int save = mapper.save(user);
            return save;
        } catch (Exception e) {
            LogUtil.handlerException(e);
        } finally {
            MybatisUtil.close(s);
        }
        return 0;
    }

    public int del(long id) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            int save = mapper.del(id);
            return save;
        } catch (Exception e) {
            LogUtil.handlerException(e);
        } finally {
            MybatisUtil.close(s);
        }
        return 0;
    }

    public int update(User user) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            int save = mapper.update(user);
            return save;
        } catch (Exception e) {
            LogUtil.handlerException(e);
        } finally {
            MybatisUtil.close(s);
        }
        return 0;
    }

    public User login(String loginname, String password) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            return mapper.login(loginname, password);
        } finally {
            MybatisUtil.close(s);
        }
    }

    public boolean checkMail(String email) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            return mapper.checkEmail(email) > 0;
        } finally {
            MybatisUtil.close(s);
        }
    }

    //返回 true，已经存在
    public boolean checkRepeat(String type, String value) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            return mapper.checkRepeat(type, value) > 0;
        } finally {
            MybatisUtil.close(s);
        }
    }

    /**
     * 根据邮件 重置密码
     *
     * @param mail
     * @param newpass
     * @return
     */
    public boolean resetPass(String mail, String newpass) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            return mapper.resetPass(mail, newpass) > 0;
        } finally {
            MybatisUtil.close(s);
        }
    }

    public boolean kofen(long id, int score) {
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper mapper = s.getMapper(UserMapper.class);
            return mapper.decrScore(id, score) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            MybatisUtil.close(s);
        }
    }

    public Questions showQuizAnswer(long userid, long qid) {
        SqlSession s = MybatisUtil.get();
        try {
            QuestionsMapper questionsMapper = s.getMapper(QuestionsMapper.class);
            Questions q = questionsMapper.getAnswer(qid);

            UserMapper mapper = s.getMapper(UserMapper.class);
            int r=mapper.decrScore(userid, q.getDegree());
            return q;
        } catch (Exception e) {
            LogUtil.handlerException(e);
        } finally {
            MybatisUtil.close(s);
        }
        return null;
    }
}
